package com.anjufang.controller.system;

import com.anjufang.controller.BaseController;
import com.anjufang.entity.AdminUser;
import com.anjufang.entity.GroupDetail;
import com.anjufang.entity.GroupInfo;
import com.anjufang.entity.SourceUser;
import com.anjufang.service.system.SourceDataUserService;
import com.anjufang.utils.Result;
import com.anjufang.utils.StringUtils;
import com.anjufang.utils.TokenManager;
import com.anjufang.utils.constant.Constant;
import com.anjufang.utils.page.PageUtils;
import com.anjufang.utils.page.Query;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/sourceUser/")
public class SourceDataUserController extends BaseController {
    @Autowired
    private SourceDataUserService sourceUserService;

    /**
     * 所有抽签用户
     */
    @RequestMapping(value = LIST)
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<SourceUser> userList = sourceUserService.queryList(query);
        int total = sourceUserService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());

        return pageUtil;
    }

    /**
     * 用户信息
     */
    @RequestMapping("info/{userId}")
    public Result info(@PathVariable("userId") Long userId) {
        SourceUser sourceUser = new SourceUser().selectOne(" id={0} and is_deleted = {1}",userId, 0);
        return Result.ok().put("sourceUser", sourceUser);
    }

    /**
     * 修改
     *
     * @return
     */
    @RequestMapping("updateInfo")
    public Result updateInfo(@RequestBody SourceUser sourceUser) {
        SourceUser sourceUser1 = new SourceUser().selectOne(" id_number = {0} and is_deleted = {1}",sourceUser.getIdNumber(),0);
        if(null!=sourceUser1){
            return Result.error("身份号码已存在");
        }
        GroupDetail groupDetail = new GroupDetail().selectOne(" user_id={0} and is_deleted = {1}",sourceUser.getId(), 0);
        if(null != groupDetail){
            return Result.error("用户已拿号，不允许修改");
        }
        sourceUser.setUpdateTime(new Date());
        sourceUser.updateById();
        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = DELETE)
    public Result delete(Long id) {
        SourceUser sourceUser = new SourceUser();
        sourceUser.setId(id);
        sourceUser.setIsDeleted(1);
        sourceUser.updateById();
        return Result.ok();
    }

}
